
package com.yanboo.generator.rest;

import com.yanboo.common.annotation.CustomController;
import com.yanboo.generator.constant.DbType;
import com.yanboo.logging.annotation.Log;
import com.yanboo.generator.domain.ProjectInfo;
import com.yanboo.generator.service.ProjectInfoService;
import com.yanboo.generator.service.dto.ProjectInfoQueryCriteria;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.jooq.SQLDialect;
import org.jooq.tools.jdbc.JDBCUtils;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.jdbc.support.JdbcUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import com.yanboo.common.utils.PageResult;
import com.yanboo.generator.service.dto.ProjectInfoDto;

/**
* @author fdq
* @date 2024-02-25
**/
@CustomController
@RequiredArgsConstructor
@Tag(name = "projectinfo管理", description = "projectinfo管理")
@RequestMapping("/api/projectInfo")
public class ProjectInfoController {

    private final ProjectInfoService projectInfoService;

    @Log("导出数据")
    @Operation(summary = "导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('projectInfo:list')")
    public void exportProjectInfo(HttpServletResponse response, ProjectInfoQueryCriteria criteria) throws IOException {
        projectInfoService.download(projectInfoService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询")
    @Operation(summary="查询")
    @PreAuthorize("@el.check('projectInfo:list')")
    public ResponseEntity<PageResult<ProjectInfoDto>> queryProjectInfo(ProjectInfoQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(projectInfoService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增")
    @Operation(summary="新增")
    @PreAuthorize("@el.check('projectInfo:add')")
    public ResponseEntity<Object> createProjectInfo(@Validated @RequestBody ProjectInfo resources){
        SQLDialect dialect = JDBCUtils.dialect(resources.getJdbcUrl());
        resources.setDbType(Enum.valueOf(DbType.class,dialect.getNameUC()));
        projectInfoService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改")
    @Operation(summary="修改")
    @PreAuthorize("@el.check('projectInfo:edit')")
    public ResponseEntity<Object> updateProjectInfo(@Validated @RequestBody ProjectInfo resources){
        projectInfoService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除")
    @Operation(summary="删除projectinfo")
    @PreAuthorize("@el.check('projectInfo:del')")
    public ResponseEntity<Object> deleteProjectInfo(@RequestBody Long[] ids) {
        projectInfoService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}